package com.glsc.ngateway.common.base.converter.platform;

import cn.hutool.core.util.StrUtil;
import com.glsc.ngateway.common.api.flowable.dto.AppendixDto;
import com.glsc.ngateway.common.base.repo.mysql.gateway.platform.AppendixRepo;
import org.apache.logging.log4j.util.Strings;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 模板转换器
 *
 * @author xuchang
 */
@Component
public class AbstractFlowFormDataConverter {

    private static AppendixRepo appendixRepo;

    @Resource
    public void setAppendixRepo(AppendixRepo appendixRepo) {
        AbstractFlowFormDataConverter.appendixRepo = appendixRepo;
    }

    protected static List<AppendixDto> converter2FileList(String fileIdList) {
        if (StrUtil.isNotBlank(fileIdList)) {
            String[] docIds = fileIdList.split(",");
            return docIds.length == 0 ? new ArrayList<>()
                    : AppendixConverter.do2Dto(Arrays.stream(docIds).map(appendixRepo::findById).collect(Collectors.toList()));
        }
        return java.util.Collections.emptyList();
    }

    protected static String converter2FileIdList(List<AppendixDto> list) {
        return CollectionUtils.isEmpty(list) ? Strings.EMPTY
                : list.stream().map(AppendixDto::getAppendixId).collect(Collectors.joining(","));
    }
}
